package com.fenghuajueli.module_user.ui.helpcenter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils
import com.fenghuajueli.libbasecoreui.utils.RouteUtils
import com.fenghuajueli.module_user.network.model.helpcenter.HelpCenterData
import com.fenghuajueli.module_user.ui.refund.RefundActivity
import com.fenghuajueli.module_user.ui.theme.ModuleDevTheme
import com.fenghuajueli.module_user.ui.theme.color0A84FF
import com.fenghuajueli.module_user.ui.widget.HelpCenterBottomWidget
import com.fenghuajueli.module_user.ui.widget.UserTopBarWidget

class HelpContentActivity : ComponentActivity() {
    companion object {
        private const val Title = "HelpContentActivityTitle"
        private const val TAG = "HelpContentActivity"

        @JvmStatic
        fun start(context: Context, title: String, center: HelpCenterData) {
            val starter = Intent(context, HelpContentActivity::class.java)
                .putExtra(Title, title)
                .putExtra(TAG, center)
            context.startActivity(starter)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val helpContent = intent.getSerializableExtra(TAG) as? HelpCenterData
            val title = intent.getStringExtra(Title) ?: ""

            ModuleDevTheme {
                Surface(
                    modifier = Modifier.fillMaxSize()
                ) {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(color = MaterialTheme.colorScheme.secondary)
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxSize()
                        ) {
                            UserTopBarWidget(
                                primary = MaterialTheme.colorScheme.primary,
                                onPrimary = MaterialTheme.colorScheme.onPrimary,
                                title = title,
                                topPadding = 0.dp,
                                onBackClick = ::finish
                            )

                            Column(
                                modifier = Modifier
                                    .weight(1f)
                                    .verticalScroll(rememberScrollState())
                            ) {
                                Spacer(modifier = Modifier.height(14.dp))

                                helpContent?.let {
                                    when(it.question){
                                        "如何申请退款？" -> RefundWidget()
                                        "在广告页面内被扣费？" -> AdWidget()
                                        else -> HelpCenterItem(it)
                                    }
                                }
                            }

                            HelpCenterBottomWidget(
                                modifier = Modifier
                            )
                        }
                    }
                }
            }
        }
    }

    @Composable
    fun HelpCenterItem(
        helpCenterItem: HelpCenterData
    ) {
        Column(
            modifier = Modifier
                .padding(horizontal = 16.dp, vertical = 6.dp)
                .fillMaxWidth()
                .background(
                    color = MaterialTheme.colorScheme.tertiary,
                    shape = RoundedCornerShape(8.dp)
                )
                .padding(start = 20.dp, end = 15.dp, top = 20.dp, bottom = 24.dp)
        ) {
            Text(
                text = helpCenterItem.question,
                fontSize = 16.sp
            )

            Spacer(modifier = Modifier.height(14.dp))

            Text(
                text = helpCenterItem.answer,
                fontSize = 14.sp,
                color = MaterialTheme.colorScheme.onSecondary
            )

            helpCenterItem.images_url.forEach {
                Spacer(modifier = Modifier.height(14.dp))

                AsyncImage(
                    modifier = Modifier
                        .fillMaxWidth(),
                    model = it,
                    contentDescription = null,
                    contentScale = ContentScale.Crop
                )
            }
        }
    }

    @Composable
    private fun RefundWidget() {
        Column(
            modifier = Modifier
                .padding(horizontal = 16.dp, vertical = 6.dp)
                .fillMaxWidth()
                .background(
                    color = MaterialTheme.colorScheme.tertiary,
                    shape = RoundedCornerShape(8.dp)
                )
                .padding(start = 20.dp, end = 15.dp, top = 20.dp, bottom = 24.dp)
        ) {
            Text(
                text = "如何申请退款？",
                fontSize = 16.sp
            )

            Spacer(modifier = Modifier.height(14.dp))

            val annotatedSubscribeString = buildAnnotatedString {
                withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.onSecondary, fontSize = 14.sp)) {
                    append("如果您对产品感到不满意，需要会员退款，请点击")
                }

                pushStringAnnotation(
                    tag = "pushString",
                    annotation = "pushString"
                )

                withStyle(style = SpanStyle(color = color0A84FF, fontSize = 14.sp)) {
                    append("【退款申请】")
                }
                pop()

                withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.onSecondary, fontSize = 14.sp)) {
                    append("填写您的资料，我们会尽快为您处理退款。\n" +
                            "感谢您对产品提出的意见建议，我们将继续完善产品体验。")
                }
            }

            ClickableText(
                modifier = Modifier,
                text = annotatedSubscribeString
            ) { offset ->
                annotatedSubscribeString.getStringAnnotations(
                    tag = "pushString",
                    start = offset,
                    end = offset
                )
                    .firstOrNull()?.let {
                        if (UserInfoUtils.getInstance().isLogin){
                            RefundActivity.start(this@HelpContentActivity)
                        }else{
                            RouteUtils.routeToRefund()
                        }
                    }
            }
        }
    }

    @Composable
    private fun AdWidget() {
        Column(
            modifier = Modifier
                .padding(horizontal = 16.dp, vertical = 6.dp)
                .fillMaxWidth()
                .background(
                    color = MaterialTheme.colorScheme.tertiary,
                    shape = RoundedCornerShape(8.dp)
                )
                .padding(start = 20.dp, end = 15.dp, top = 20.dp, bottom = 24.dp)
        ) {
            Text(
                text = "在广告页面内被扣费？",
                fontSize = 16.sp
            )

            Spacer(modifier = Modifier.height(14.dp))

            val annotatedSubscribeString = buildAnnotatedString {
                withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.onSecondary, fontSize = 14.sp)) {
                    append("1.广告投诉须知：应用内广告均来自于各大公司的广告推送。广告商非本站来源，请谨慎充值与付费。\n" +
                            "2.如有订单疑问或退款需求，可在对应广告页面；扣费通知短信；支付宝/微信账单详情页商品说明里找到商家联系方式，直接向商家要求退款。若您签约的是自动续费服务，可到支付宝/微信-支付设置中，停止对应的自动续费服务。具体指引请查看下方的“快速自助广告扣费退款”。\n" +
                            "3.如果沟通过程中遇到困难，需要我方协助处理，请您点击下方的“联系客服”，说明问题诉求并提供以下信息。方便我们与广告平台沟通，尽快联系到商家为您解决问题。\n\n")
                }

                withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.onSecondaryContainer, fontSize = 14.sp)) {
                    append("（1）您的支付宝/微信付款订单截图\n" +
                            "（2）您的支付宝账号/微信账号（账号需要有绑定手机号）\n" +
                            "（3）您的联系方式（方便我们向您同步处理进度）\n" +
                            "收到信息后，我方会协助您提交广告平台进行核实，平台核实时间约为2-5个工作日，核实完成后1个工作日内平台会安排商家进行退款。\n" +
                            "感谢您的支持与谅解！\n\n")
                }

                withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.onSecondary, fontSize = 14.sp)) {
                    append("快速自助广告扣费退款：找到支付订单 - 拨打商家客服电话退费 \n\n")
                }

                withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.onSecondaryContainer, fontSize = 14.sp)) {
                    append("1.使用微信支付：\n" +
                            "（1）打开微信点击“我” - 点击“服务” - 点击右上角“账单”\n" +
                            "（2）找到对应的订单点击进入\n" +
                            "（3）查看账单上的商家客服电话\n" +
                            "（4）直接拨打商家客服电话咨询/退款，更快捷高效！\n\n" +
                            "2.使用支付宝支付：\n" +
                            "（1）打开支付宝点击“我的” - 点击“账单” \n" +
                            "（2）找到对应的订单点击进入\n" +
                            "（3）查看账单上的商家客服电话\n" +
                            "（4）直接拨打商家客服电话咨询/退款，更快捷高效！\n\n" +
                            "3.其他方式：\n" +
                            "若您未在支付订单，找到商家客服联系方式。您可考虑回到扣费广告的页面，查看该页面中的“服务说明”，找到商家客服联系方式。或若您有收到扣费通知短信，可查看短信中显示的商家客服联系方式。")
                }
            }

            ClickableText(
                modifier = Modifier,
                text = annotatedSubscribeString
            ) { offset ->
                annotatedSubscribeString.getStringAnnotations(
                    tag = "pushString",
                    start = offset,
                    end = offset
                )
                    .firstOrNull()?.let {
                        ToastUtils.showLong(it.item)
                    }
            }
        }
    }
}